/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import enun.Perfil;
import java.util.Date;
import javax.swing.JOptionPane;
import model.AgendarConsulta;
import model.Dentista;
import model.Endereco;
import model.Login;
import model.Paciente;

/**
 *
 * @author Rodrigo
 */
public class ViewConsultarConsultasGuia extends javax.swing.JFrame {
    
    private int num = 0;
    AgendarConsulta agenda = new AgendarConsulta();
    private Login login;
    
    public ViewConsultarConsultasGuia() {
        this(new Login());
        
    }
    
    public ViewConsultarConsultasGuia(Login l) {
        this.login = l;
        initComponents();
        lblLogado.setText(login.getPerfil().getDescricao());
        carregarTable();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbGUIA = new javax.swing.JTable();
        btnSair = new javax.swing.JButton();
        txtNome = new javax.swing.JTextField();
        lblNome = new javax.swing.JLabel();
        btnPesquisar = new javax.swing.JButton();
        dateChooser = new com.toedter.calendar.JDateChooser();
        lblData = new javax.swing.JLabel();
        btnLimpar = new javax.swing.JButton();
        lblusuarioLogado = new javax.swing.JLabel();
        lblLogado = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        tbGUIA.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, null, new java.awt.Color(204, 204, 255), null, null));
        tbGUIA.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        tbGUIA.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tbGUIA.setGridColor(new java.awt.Color(51, 153, 255));
        tbGUIA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbGUIAMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbGUIA);

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        lblNome.setText("Digite um nome para pesquisa:");

        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        lblData.setText("Informe a data para pesquisa:");

        btnLimpar.setText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        lblusuarioLogado.setText("Usuario logado:");

        lblLogado.setText("-");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnSair)
                .addGap(20, 20, 20))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1255, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(lblNome))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(lblData)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnLimpar)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnPesquisar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblusuarioLogado)
                                .addGap(18, 18, 18)
                                .addComponent(lblLogado, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(9, 9, 9)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(28, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNome)
                            .addComponent(btnPesquisar))
                        .addGap(9, 9, 9))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblusuarioLogado, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblLogado))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblData)
                    .addComponent(btnLimpar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated

    }//GEN-LAST:event_formWindowActivated

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        if (verificarCampoPesquisa()) {
            carregarTable();
        } else {
            JOptionPane.showMessageDialog(null, "Informe pelo menos um filtro para pesquisa");
        }
    }//GEN-LAST:event_btnPesquisarActionPerformed

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        limparTela();
        carregarTable();
    }//GEN-LAST:event_btnLimparActionPerformed

    private void tbGUIAMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbGUIAMouseClicked
        carregarInformacaoConsulta(evt);
    }//GEN-LAST:event_tbGUIAMouseClicked
    
    private void limparTela() {
        txtNome.setText("");
        dateChooser.setDate(null);
    }
    
    private boolean verificarCampoPesquisa() {
        boolean isvalido = false;
        if (!(txtNome.getText().equals(""))) {
            isvalido = true;
        } else if (dateChooser.getDate() != null) {
            isvalido = true;
        }
        return isvalido;
    }
    
    private void carregarTable() {
        TableGuia tbGuia = new TableGuia();        
        tbGuia.consultarFiltro(txtNome.getText(), dateChooser.getDate());
        tbGUIA.setModel(tbGuia);
        
    }
    
    private void carregarInformacaoConsulta(java.awt.event.MouseEvent evt) {
        ViewGuia gui = new ViewGuia(login);
        num = tbGUIA.getSelectedRow();
        if (evt.getClickCount() == 2 && login.getPerfil() == Perfil.SECRETARIA_ADM) {
            agenda.setId((Integer) tbGUIA.getValueAt(num, 0));
            agenda.setDataConsulta((Date) tbGUIA.getValueAt(num, 1));
            agenda.setDescricao((String) tbGUIA.getValueAt(num, 3));
            agenda.setHora((String) tbGUIA.getValueAt(num, 2));
            agenda.setPaciente((Paciente) tbGUIA.getValueAt(num, 4));
            agenda.getPaciente().setEndereco((Endereco) tbGUIA.getValueAt(num, 5));
            agenda.setDentista((Dentista) tbGUIA.getValueAt(num, 6));
            gui.setModel(agenda);
            gui.setVisible(true);
            this.setVisible(false);
        }
        
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLimpar;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JButton btnSair;
    private com.toedter.calendar.JDateChooser dateChooser;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblData;
    private javax.swing.JLabel lblLogado;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblusuarioLogado;
    private javax.swing.JTable tbGUIA;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables
}
